<?php
define('MIN_FQDN_LENGTH', 4);
define('MAX_FQDN_LENGTH', 255);
define('MAX_LPART_LENGTH', 64);

class Tools {

// Преобразования
    public static function utf2ansi($s) { return $s ? iconv('UTF-8','CP1251',$s) : ''; }
    public static function ansi2utf($s) { return $s ? iconv('CP1251','UTF-8',$s) : ''; }

// Проверки
    public static function is_boolstr( $s ) {
        $s = strtolower($s);
        return is_string( $s ) && ( $s == 'true' || $s == 'false' );
    }
    
    public static function is_numeric_array( $arr ) {
        $res = true;
        if( !count($arr) ) return false;
        foreach( $arr as $value ) {
            if( !is_numeric($value) ) { $res = false; break; }
        }
        return $res;
    }

    public static function check_referer() {
        return preg_match("|^http(s)?://".$_SERVER['SERVER_NAME']."/|", $_SERVER['HTTP_REFERER']) ? true : false;
    }
    
// Ajax & json
    public static function check_ajax_referer() {
        return ( preg_match("|^http(s)?://".$_SERVER['SERVER_NAME']."/|", $_SERVER['HTTP_REFERER']) &&
                isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 
                strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ) ? true : false;
    }
    public static function json_response( $errmess, $errcode=0, $data = null ) {
        $json_arr = array( 
                'errcode'=>$errcode, 
                'errmess'=>$errmess,
                'data'=>$data 
                );
        header("Content-type: text/script;charset=utf-8");
        echo json_encode( $json_arr );   
    }
    
//
// Check e-mail address
//
    public static function is_mail_address( $mail ) {
        if( !is_string($mail) || $mail === '' )
        return false;
        
        $mail = strtolower($mail);
        $uxo = strpos( $mail, '@');
        
        $lpart = '';
        $domain = '';
    
        $result = false;
        while( true ) {
    
            if( !$uxo ) break;    
            
            if( $uxo > 0 && $uxo < strlen($mail) - 1 ) {
                $lpart = substr( $mail,0,$uxo );
                $domain = substr( $mail,$uxo+1);
            
                $mlen = strlen( $lpart );
                if( $mlen > MAX_LPART_LENGTH ) break;
                if( !preg_match( '/^[a-z0-9_](?:[0-9a-z._+-]*[a-z0-9_])?$/', $lpart ) )
                    break;
            
                $mlen = strlen( $domain );        
                if( $mlen > MAX_FQDN_LENGTH || $mlen < MIN_FQDN_LENGTH ) 
                    break;
            
                $result = preg_match( '/^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z*]{2,}$/', $domain );
                    break;
            }
        
            break;
        }
        
        return $result;
    }
    
//
// Generate password
//
	public static function passw_gen( $max ) {
		$chars="qazxswedcvfrtgbnhyujmkiolp1234567890QAZXSWEDCVFRTGBNHYUJMKIOLP";
		$size=strlen($chars)-1;

		$password='';
		while($max--)
			$password.=$chars[rand(0,$size)];
			
		return $password;
	}
//
// Sent plain/text e-mail
//
    public static function mail_utf8($to, $subject = '(No subject)', $message = '', $header = '') {
        $header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
        mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_ . $header, "-fnoreply@ipdepo.com");
    } 
    
}
?>